package com.suredy.app.porject.ctrl.view;

import java.util.ArrayList;
import java.util.List;

import com.suredy.app.porject.roles.SystemAtomPower;
import com.suredy.security.model.User;

/**
 * 视图权限控制
 * 
 * @author VIVID.G
 * @since 2016-1-26
 * @version v0.1
 */
public class ViewRoleCtrl {

	protected User user;
	protected List<String> roleCodes;

	public ViewRoleCtrl(User user, List<String> roleCodes) {
		this.user = user;

		if (roleCodes == null)
			roleCodes = new ArrayList<String>();

		this.roleCodes = roleCodes;
	}

	// 获取系统配置的公司索引
	public Integer getCompanyIndex() {
		if (this.isTxPeople())
			return 0;

		if (this.isZzPeople())
			return 1;

		return 9;
	}

	// 系统管理员
	public boolean isSystemManager() {
		return this.isHasRole(SystemAtomPower.p_system_manager);
	}

	// 天星人员
	public boolean isTxPeople() {
		return this.isHasRole(SystemAtomPower.p_people_tx);
	}

	// 众志人员
	public boolean isZzPeople() {
		return this.isHasRole(SystemAtomPower.p_people_zz);
	}

	// 非天星、众志人员
	public boolean isOtherPeople() {
		return !this.isTxPeople() && !this.isZzPeople();
	}

	// 新建投标
	public boolean isCanCreateBid() {
		return this.isHasRole(SystemAtomPower.p_bid_creator);
	}

	// 修改投标信息
	public boolean isCanEditBid() {
		return this.isHasRole(SystemAtomPower.p_bid_editor);
	}

	// 删除投标信息
	public boolean isCanDelBid() {
		return this.isHasRole(SystemAtomPower.p_bid_deleter);
	}

	// 管理投标
	public boolean isCanManageBid() {
		return this.isHasRole(SystemAtomPower.p_bid_manager);
	}

	// 主合同
	public boolean isContractMain() {
		return this.isHasRole(SystemAtomPower.p_contract_main);
	}

	// 任务书
	public boolean isContractTask() {
		return this.isHasRole(SystemAtomPower.p_contract_task);
	}

	// 材料
	public boolean isContractMaterial() {
		return this.isHasRole(SystemAtomPower.p_contract_material);
	}

	// 劳务分包
	public boolean isContractService() {
		return this.isHasRole(SystemAtomPower.p_contract_service);
	}

	// 专业分包
	public boolean isContractProfessional() {
		return this.isHasRole(SystemAtomPower.p_contract_professional);
	}

	// 补充协议
	public boolean isContractAgreement() {
		return this.isHasRole(SystemAtomPower.p_contract_agreement);
	}

	// 子合同（不包含补充协议）
	public boolean isContractSub() {
		boolean is = this.isContractTask();
		is = is || this.isContractMaterial();
		is = is || this.isContractService();
		is = is || this.isContractProfessional();

		return is;
	}

	// 创建合同
	public boolean isCanCreateContract() {
		return this.isHasRole(SystemAtomPower.p_contract_creator);
	}

	// 修改合同
	public boolean isCanEditorContract() {
		return this.isHasRole(SystemAtomPower.p_contract_editor);
	}

	// 删除合同
	public boolean isCanDelContract() {
		return this.isHasRole(SystemAtomPower.p_contract_deleter);
	}

	// 合同管理
	public boolean isCanManageContract() {
		return this.isHasRole(SystemAtomPower.p_contract_manager);
	}

	// 接收签订的主合同
	public boolean isCanReceiveContract() {
		return this.isHasRole(SystemAtomPower.p_contract_receiver);
	}

	// 收款计划
	public boolean isCanPlanInMoney() {
		return this.isHasRole(SystemAtomPower.p_money_in_plan);
	}

	// 收款
	public boolean isCanInMoney() {
		return this.isHasRole(SystemAtomPower.p_money_in);
	}

	// 付款计划
	public boolean isCanPlanOutMoney() {
		return this.isHasRole(SystemAtomPower.p_money_out_plan);
	}

	// 付款
	public boolean isCanOutMoney() {
		return this.isHasRole(SystemAtomPower.p_money_out);
	}

	// 工期计划
	public boolean isCanPlanWork() {
		return this.isHasRole(SystemAtomPower.p_work_plan);
	}

	// 履行工期
	public boolean isCanWork() {
		return this.isHasRole(SystemAtomPower.p_work);
	}

	// 统计-合同资金情况
	public boolean isCanViewCountHtMoney() {
		return this.isHasRole(SystemAtomPower.p_count_ht_money);
	}

	// 统计-经营资金情况
	public boolean isCanViewCountJyMoney() {
		return this.isHasRole(SystemAtomPower.p_count_jy_money);
	}

	// 统计-合同资金占比
	public boolean isCanViewCountHtMoneyRatio() {
		return this.isHasRole(SystemAtomPower.p_count_ht_money_ratio);
	}

	// 统计-客户项目数量占比
	public boolean isCanViewCountHtCustomer() {
		return this.isHasRole(SystemAtomPower.p_count_ht_customer);
	}

	// 统计-投标情况统计
	public boolean isCanViewCountBid() {
		return this.isHasRole(SystemAtomPower.p_count_bid);
	}

	// 统计-合同签订情况统计
	public boolean isCanViewCountHt() {
		return this.isHasRole(SystemAtomPower.p_count_ht);
	}

	// 统计-合同履行情况统计
	public boolean isCanViewCountHtExe() {
		return this.isHasRole(SystemAtomPower.p_count_ht_exe);
	}

	// 统计-费用支出
	public boolean isCanViewCountMoneyCost() {
		return this.isHasRole(SystemAtomPower.p_count_money_cost);
	}

	// 合同-保证金管理
	public boolean isBzjManager() {
		return this.isHasRole(SystemAtomPower.p_contract_bzj);
	}

	// 成本-费用支出
	public boolean isCBPayout() {
		return this.isHasRole(SystemAtomPower.p_cb_payout);
	}

	// 财务-结算确认
	public boolean isCwConfirmForClearint() {
		return this.isHasRole(SystemAtomPower.p_cw_clearing_confirm);
	}

	// 财务-成本确认
	public boolean isCwConfirmForPayout() {
		return this.isHasRole(SystemAtomPower.p_cw_payout_confirm);
	}

	// ========== 一下为业务权限（复合权限） ========== //

	// 创建主合同
	public boolean isCanCreateMainContract() {
		return this.isCanCreateContract() && this.isContractMain();
	}

	// 创建任务书
	public boolean isCanCreateTaskContract() {
		return this.isCanCreateContract() && this.isContractTask();
	}

	// 创建材料合同
	public boolean isCanCreateMaterialContract() {
		return this.isCanCreateContract() && this.isContractMaterial();
	}

	// 创建劳务分包合同
	public boolean isCanCreateServiceContract() {
		return this.isCanCreateContract() && this.isContractService();
	}

	// 创建专业分包合同
	public boolean isCanCreateProfessionalContract() {
		return this.isCanCreateContract() && this.isContractProfessional();
	}

	// 创建补充协议
	public boolean isCanCreateAgreementContract() {
		return this.isCanCreateContract() && this.isContractAgreement();
	}

	protected boolean isHasRole(String role) {
		if (this.roleCodes == null || this.roleCodes.isEmpty())
			return false;

		return this.roleCodes.contains(role);
	}

}
